package cn.itlight.order.controller;

import cn.itlight.basic.exception.BusinessException;
import cn.itlight.order.dto.OrderConfirmDto;
import cn.itlight.order.service.IOrderProductAcquisitionService;
import cn.itlight.order.domain.OrderProductAcquisition;
import cn.itlight.order.query.OrderProductAcquisitionQuery;
import cn.itlight.basic.utils.AjaxResult;
import cn.itlight.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderProductAcquisition")
public class OrderProductAcquisitionController {
    @Autowired
    public IOrderProductAcquisitionService orderProductAcquisitionService;


    /**
     * 保存和修改公用的
     * @param orderProductAcquisition  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderProductAcquisition orderProductAcquisition){
        try {
            if( orderProductAcquisition.getId()!=null)
                orderProductAcquisitionService.update(orderProductAcquisition);
            else
                orderProductAcquisitionService.save(orderProductAcquisition);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderProductAcquisitionService.cancelOrder(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public OrderProductAcquisition get(@PathVariable("id")Long id)
    {
        return orderProductAcquisitionService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderProductAcquisition> list(){

        return orderProductAcquisitionService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<OrderProductAcquisition> json(@RequestBody OrderProductAcquisitionQuery query)
    {
        return orderProductAcquisitionService.queryData(query);
    }

    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderConfirmDto orderPetAcquisitionDto){
        try {
            orderProductAcquisitionService.confirm(orderPetAcquisitionDto);
            return AjaxResult.me();
        }catch(BusinessException e){
            e.printStackTrace();
            // 如果是自定义异常，那么返回给前端的提示语就是service层设置的提示语
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！！！");
        }
    }
}
